<?php

//Loader::loadClass('SecurityUtilEx', "modules/VotePortal/pnincludes");

/**
* //////////////////////////////////////////////////
* auto execute , for initialize config variable
* this function auto call every page has been fetch
*/
function _autoexecute(){

}

function VotePortal_xml_permission()
{
  // Security check
  //we are allow for admin access level , see in config.php variable name ACCESS_EDIT
  if (!SecurityUtil::checkPermission('VotePortal::', '::', ACCESS_EDIT)) {
      LogUtil::registerPermissionError(pnModUrl('Users','user','loginscreen'));
  }
}

/**
* smarty template auto call before render
*/
function _preRender(&$render){
  $lang    = FormUtil::getPassedValue ('lang', false , 'GET');
  $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

  $render->assign ('_GET', $_GET);
  $render->assign ('_POST', $_POST);
  $render->assign ('_REQUEST', $_REQUEST);

  $render->assign('ctrl', $ctrl);
  //$render->assign('setting',InnoUtil::getSetting());
  $render->assign('user',   InnoUtil::getUserInfo());

  if ($lang){
    $render->assign('lang', $lang);
  }else{
    $render->assign('lang', pnUserGetLang());
  }
  $render->assign('access_edit', SecurityUtil::checkPermission('VotePortal::', '::', ACCESS_EDIT));
}

/**
 * Main user function . the user side page controller
 * @author Chayakon PONGSIRI
 * @return string HTML string
 */
function VotePortal_xml_main() {
    //_autoexecute();
    return VotePortal_xml_page();
}

function VotePortal_xml_georss()
{  
  
  $par_id  = FormUtil::getPassedValue ('par_id', null , 'GET');

  //pagnigation variable
  $sort       = FormUtil::getPassedValue ('sort', null , 'GET');
  $pagesize   = 100;
  $page       = FormUtil::getPassedValue ('page', null , 'GET');
     
  $render = pnRender::getInstance('VotePortal');

  if (!($class = Loader::loadClassFromModule ('VotePortal','VillagePoll', true)))
    return LogUtil::registerError ("Unable to load class [VillagePoll] ...");

  $objectArray = new $class ();
  $where   = null;
  $sort = null;
  if(isset($par_id)){
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
  }

  if (empty($where)){
    $where = implode(' AND ',$objectArray->_objWhere);
  }else if (is_array($objectArray->_objWhere)){
    $where .= implode(' AND ',$objectArray->_objWhere);
  }else if (!empty($objectArray->_objWhere)){
    $where .= ' AND ' . $objectArray->_objWhere;
  }
  if (empty($sort)){
    $sort = $objectArray->_objSort;
  }


  //Spilt page
  $pager = array();
  $totalItems  = $objectArray->getCount($where , true);
  $totalPages  = ceil($totalItems/$pagesize);
  $nowPage = $page;
  if($page>0)
  $page -= 1;
  $render->assign ('pager', $pager);
  $objectArray->get ($where, $sort , $page, $pagesize);
  $objDataArray = $objectArray->_objData;
  
  if(intval($par_id)&&isset($par_id))
  {
    $args = "&par_id=".$par_id;
  }


  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  $xmlRss = $xml->createElement("rss");
  
  
  $xmlRss->setAttribute("version",2.0);
  $xmlRss->setAttribute("xmlns:atom","http://www.w3.org/2005/Atom");
  $xmlRss->setAttribute("xmlns:georss","http://www.georss.org/georss");
  
  $xmlChannel = $xml->createElement("channel");
  
  $xmlCTitle = $xml->createElement("title","VotePortal");
  $xmlChannel->appendChild($xmlCTitle);
  
  $xmlCLink = $xml->createElement("link","Poll.VoteThailand.com");
  $xmlChannel->appendChild($xmlCLink);
  
  $xmlDate = $xml->createElement("pubDate",date("Y-m-d H:i:s", time()));
  $xmlChannel->appendChild($xmlDate);
  
  $xmlAtom = $xml->createElement("atom:link");
  $xmlChannel->appendChild($xmlAtom);
  
  $xmlAtom->setAttribute("href","http://www.911thai.com/feed/");
  $xmlAtom->setAttribute("rel","self");
  $xmlAtom->setAttribute("type","application/rss+xml");
  
  $xmlTotalItem = $xml->createElement("totalItems",$totalItems);
  $xmlChannel->appendChild($xmlTotalItem);
    
  $xmlTotalPages = $xml->createElement("totalPages",$totalPages);
  $xmlChannel->appendChild($xmlTotalPages);
  

  if(($nowPage>0)&&($nowPage<=$totalPages))
  {
    if($nowPage==1||($nowPage>$totalPages))
    {
      $previousPage = '';
    }else{
      $previousPage = htmlentities('poll.votethailand.com/index.php?module=VotePortal&type=user&func=rss&page='.($nowPage-1).$args);
    }

    if(($nowPage>=$totalPages))
    {
      $nextPage = '';
    }else{
      $nextPage = htmlentities('poll.votethailand.com/index.php?module=VotePortal&type=user&func=rss&page='.($nowPage+1).$args);
    }
    
  }
  if($nowPage<1)
  $nowPage = 1;
  $xmlNowPage = $xml->createElement("nowPage",$nowPage);
  $xmlChannel->appendChild($xmlNowPage);
  
  $xmlPreviousPage = $xml->createElement("previousPage",$previousPage);
  $xmlChannel->appendChild($xmlPreviousPage);
  
  $xmlNextPage = $xml->createElement("nextPage",$nextPage);
  $xmlChannel->appendChild($xmlNextPage);
  
  foreach($objDataArray as $val)
  {
    
    $xmlChild = $xml->createElement("item");

    $xmlTitle = $xml->createElement("title","บ้านเลขที่ ".$val['house_no']);
    $xmlChild->appendChild($xmlTitle);
    
    $xmlPath = $xml->createElement("link",htmlentities('http://poll.votethailand.com/index.php?module=VotePortal&func=viewrss&id='.$val['id']));
    $xmlChild->appendChild($xmlPath);
    
    $xmlGeo = $xml->createElement("georss:point",$val['latitude']." ".$val['longitude']);
    $xmlChild->appendChild($xmlGeo);
    
    $xmlDes = $xml->createElement("description",$val['comment']);
    $xmlChild->appendChild($xmlDes);
    
    $xmlGuid = $xml->createElement("guid",$val['id']);
    $xmlChild->appendChild($xmlGuid);
    
    $xmlDate = $xml->createElement("pubDate",$val['lu_date']);
    $xmlChild->appendChild($xmlDate);
    
    $xmlChannel->appendChild($xmlChild);
  }

  $xmlRss->appendChild($xmlChannel);
  $xml->appendChild($xmlRss);
  
  
  print $xml->saveXML();
  pnShutDown();
  
}

function VotePortal_xml_province()
{
  $pid  = FormUtil::getPassedValue ('pid', null , 'GET');
  $list  = FormUtil::getPassedValue ('list', null , 'GET');

  if(isset($pid)&&intval($pid)){
    //if assign $pid (means, reguester want to see detail of it)
    if (!($class = Loader::loadClassFromModule ('VotePortal','Province', true)))
    return LogUtil::registerError ("Unable to load class [Province] ...");

    $object = new $class ();
    $where   = null;
    $sort = null;

    if(isset($pid)){
      if (method_exists($object,'genFilter')){
        $where = $object->genFilter();
      }
  	}

    if (empty($where)){
      $where = implode(' AND ',$object->_objWhere);
    }else if (is_array($object->_objWhere)){
      $where .= implode(' AND ',$object->_objWhere);
    }else if (!empty($object->_objWhere)){
      $where .= ' AND ' . $object->_objWhere;
    }
    if (empty($sort)){
      $sort = $object->_objSort;
    }
    $object->get($where);
    $objData = $object->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    $i=0;
    foreach($objData as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id",++$i);
  
      $xmlTitle = $xml->createElement("title","infomation of จ.".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlDes = $xml->createElement("description","Description of จ.".$val['name']);
      $xmlChild->appendChild($xmlDes);
      
      $xmlDetail = $xml->createElement("detail","Detail of ".$val['name']);
      $xmlChild->appendChild($xmlDetail);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
    
  }else if(!isset($pid)){
    //if not assign $pid, some time input incorrect $pid it's not comes to this case.
    if (!($class = Loader::loadClassFromModule ('VotePortal','ProvinceArray', false)))
    return LogUtil::registerError ("Unable to load class [ProvinceArray] ...");

    $objectArray = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($list))
    {
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
    }
    
    if (empty($sort)){
      $sort = $objectArray->_objSort;
    }
    $objectArray->get ($where, $sort , $page, $pagesize);
    $objDataArray = $objectArray->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    
    if(empty($objDataArray))
    {
      $xmlChild = $xml->createElement("data");
      $xmlChild->setAttribute("id",0);
      $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
      $xmlChild->appendChild($xmlTitle);
      $xmlDatas->appendChild($xmlChild);
      $xml->appendChild($xmlDatas);
    
      print $xml->saveXML();
      pnShutDown();
    }
    
    foreach($objDataArray as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id","pv".sprintf("%06d",$val['id']));
  
      $xmlTitle = $xml->createElement("title","จ.".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlCountry = $xml->createElement("country","ประเทศไทย");
      $xmlChild->appendChild($xmlCountry);
      
      $xmlLogo = $xml->createElement("logo",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'modules/VotePortal/pnimages/province/'.$val['id'].".png"));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlPop = $xml->createElement("population","300000");
      $xmlChild->appendChild($xmlPop);
      
      $xmlLat = $xml->createElement("latitude",$val['latitude']);
      $xmlChild->appendChild($xmlLat);
      
      $xmlLon = $xml->createElement("longitude",$val['longitude']);
      $xmlChild->appendChild($xmlLon);
      
      $xmlSpan = $xml->createElement("span",0.4);
      $xmlChild->appendChild($xmlSpan);
      
      $xmlInfo = $xml->createElement("infopath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=province&pid='.$val['id']));
      $xmlChild->appendChild($xmlInfo);
      
      $xmlReport = $xml->createElement("reportpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=report&pid='.$val['id']));
      $xmlChild->appendChild($xmlReport);
      
      $xmlCurrent = $xml->createElement("xmlcurrent",'http://www.phuketinnova.com/demo/gthai/pv00001.xml');
      $xmlChild->appendChild($xmlCurrent);
      
      $xmlPath = $xml->createElement("xmlpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=district&pid='.$val['id']));
      $xmlChild->appendChild($xmlPath);
      
      $xmlUpdate = $xml->createElement("xmlupdate",'ProvinceData');
      $xmlChild->appendChild($xmlUpdate);
      
      $xmlDate = $xml->createElement("date",$val['lu_date']);
      $xmlChild->appendChild($xmlDate);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
  }else{
    //other case, we don't show anything.
    pnShutDown();
  }
  
  pnShutDown();
  
}

function VotePortal_xml_district()
{
  $pid  = FormUtil::getPassedValue ('pid', null , 'GET');
  $did  = FormUtil::getPassedValue ('did', null , 'GET');
  $page  = (FormUtil::getPassedValue ('page', null , 'GET')) ? FormUtil::getPassedValue ('page', null , 'GET') : 0;
  $pagesize = (FormUtil::getPassedValue ('pagesize', null , 'GET')) ? FormUtil::getPassedValue ('pagesize', null , 'GET') : 1000;
  
  if((!isset($pid)&&isset($did))){
    //if assign $pid (means, reguester want to see detail of it) render district list of this province.
    if (!($class = Loader::loadClassFromModule ('VotePortal','District', true)))
    return LogUtil::registerError ("Unable to load class [District] ...");

    $object = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($did)){
      if (method_exists($object,'genFilter')){
        $where = $object->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$object->_objWhere);
    }else if (is_array($object->_objWhere)){
      $where .= implode(' AND ',$object->_objWhere);
    }else if (!empty($object->_objWhere)){
      $where .= ' AND ' . $object->_objWhere;
    }
    if (empty($sort)){
      $sort = $object->_objSort;
    }
    $object->get($where);
    $objData = $object->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    $i=0;
    foreach($objData as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id",++$i);
  
      $xmlTitle = $xml->createElement("title","infomation of อ.".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlDes = $xml->createElement("description","Description of อ.".$val['name']);
      $xmlChild->appendChild($xmlDes);
      
      $xmlDetail = $xml->createElement("detail","Detail of อ.".$val['name']);
      $xmlChild->appendChild($xmlDetail);
      
      $xmlDatas->appendChild($xmlChild);
    }
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
    
  }else if(isset($pid)&&(!isset($did))){
    //if not assign $pid, some time input incorrect $pid it's not comes to this case.
    if (!($class = Loader::loadClassFromModule ('VotePortal','DistrictArray', false)))
    return LogUtil::registerError ("Unable to load class [DistrictArray] ...");

    $objectArray = new $class ();
    $where   = null;
    if(isset($pid)){
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$objectArray->_objWhere);
    }else if (is_array($objectArray->_objWhere)){
      $where .= implode(' AND ',$objectArray->_objWhere);
    }else if (!empty($objectArray->_objWhere)){
      $where .= ' AND ' . $objectArray->_objWhere;
    }
    if (empty($sort)){
      $sort = $objectArray->_objSort;
    }
    $objectArray->get ($where);
    $objDataArray = $objectArray->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    
    if(empty($objDataArray))
    {
      $xmlChild = $xml->createElement("data");
      $xmlChild->setAttribute("id",0);
      $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
      $xmlChild->appendChild($xmlTitle);
      $xmlDatas->appendChild($xmlChild);
      $xml->appendChild($xmlDatas);
    
      print $xml->saveXML();
      pnShutDown();
    }
    
    foreach($objDataArray as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id","dt".sprintf("%06d",$val['id']));
  
      $xmlTitle = $xml->createElement("title","อ.".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlPop = $xml->createElement("population","30000");
      $xmlChild->appendChild($xmlPop);
      
      $xmlLat = $xml->createElement("latitude",$val['latitude']);
      $xmlChild->appendChild($xmlLat);
      
      $xmlLon = $xml->createElement("longitude",$val['longitude']);
      $xmlChild->appendChild($xmlLon);
      
      $xmlSpan = $xml->createElement("span",0.3);
      $xmlChild->appendChild($xmlSpan);
      
      $xmlInfo = $xml->createElement("infopath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=district&did='.$val['id']));
      $xmlChild->appendChild($xmlInfo);
      
      $xmlReport = $xml->createElement("reportpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=report&did='.$val['id']));
      $xmlChild->appendChild($xmlReport);
      
      $xmlCurrent = $xml->createElement("xmlcurrent",'http://www.phuketinnova.com/demo/gthai/pv00001.xml');
      $xmlChild->appendChild($xmlCurrent);
      
      $xmlPath = $xml->createElement("xmlpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=locality&did='.$val['id']));
      $xmlChild->appendChild($xmlPath);
      
      $xmlUpdate = $xml->createElement("xmlupdate",'DistrictData');
      $xmlChild->appendChild($xmlUpdate);
      
      $xmlDate = $xml->createElement("date",$val['lu_date']);
      $xmlChild->appendChild($xmlDate);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
  }else{
    //other case, we don't show anything.
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("error");
    $xmlChild = $xml->createElement("message",'Inccorect argument:[pid],[did] are require.');
    $xmlDatas->appendChild($xmlChild);
  
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  
  pnShutDown();
  
}

function VotePortal_xml_locality()
{
  $did  = FormUtil::getPassedValue ('did', null , 'GET');
  $lid  = FormUtil::getPassedValue ('lid', null , 'GET');
  $page  = (FormUtil::getPassedValue ('page', null , 'GET')) ? FormUtil::getPassedValue ('page', null , 'GET') : 0;
  $pagesize = (FormUtil::getPassedValue ('pagesize', null , 'GET')) ? FormUtil::getPassedValue ('pagesize', null , 'GET') : 1000;
  
  if((!isset($did)&&isset($lid))){
    //if assign $did (means, reguester want to see detail of it) render district list of this province.
    if (!($class = Loader::loadClassFromModule ('VotePortal','Tambon', true)))
    return LogUtil::registerError ("Unable to load class [Tambon] ...");

    $object = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($lid)){
      if (method_exists($object,'genFilter')){
        $where = $object->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$object->_objWhere);
    }else if (is_array($object->_objWhere)){
      $where .= implode(' AND ',$object->_objWhere);
    }else if (!empty($object->_objWhere)){
      $where .= ' AND ' . $object->_objWhere;
    }
    if (empty($sort)){
      $sort = $object->_objSort;
    }
    $object->get($where);
    $objData = $object->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    $i=0;
    foreach($objData as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id",++$i);
  
      $xmlTitle = $xml->createElement("title","infomation of ต.".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlDes = $xml->createElement("description","Description of ต.".$val['name']);
      $xmlChild->appendChild($xmlDes);
      
      $xmlDetail = $xml->createElement("detail","Detail of ต.".$val['name']);
      $xmlChild->appendChild($xmlDetail);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
    
  }else if(isset($did)&&(!isset($lid))){
    //if not assign $did, some time input incorrect $did it's not comes to this case.
    if (!($class = Loader::loadClassFromModule ('VotePortal','TambonArray', false)))
    return LogUtil::registerError ("Unable to load class [TambonArray] ...");

    $objectArray = new $class ();
    $where   = null;
    if(isset($did)){
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$objectArray->_objWhere);
    }else if (is_array($objectArray->_objWhere)){
      $where .= implode(' AND ',$objectArray->_objWhere);
    }else if (!empty($objectArray->_objWhere)){
      $where .= ' AND ' . $objectArray->_objWhere;
    }
    if (empty($sort)){
      $sort = $objectArray->_objSort;
    }
    $objectArray->get ($where);
    $objDataArray = $objectArray->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    
    if(empty($objDataArray))
    {
      $xmlChild = $xml->createElement("data");
      $xmlChild->setAttribute("id",0);
      $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
      $xmlChild->appendChild($xmlTitle);
      $xmlDatas->appendChild($xmlChild);
      $xml->appendChild($xmlDatas);
    
      print $xml->saveXML();
      pnShutDown();
    }
    
    foreach($objDataArray as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id","lc".sprintf("%06d",$val['id']));
  
      $xmlTitle = $xml->createElement("title","ต.".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlPop = $xml->createElement("population","3000");
      $xmlChild->appendChild($xmlPop);
      
      $xmlLat = $xml->createElement("latitude",$val['latitude']);
      $xmlChild->appendChild($xmlLat);
      
      $xmlLon = $xml->createElement("longitude",$val['longitude']);
      $xmlChild->appendChild($xmlLon);
      
      $xmlSpan = $xml->createElement("span",0.2);
      $xmlChild->appendChild($xmlSpan);
      
      $xmlInfo = $xml->createElement("infopath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=locality&lid='.$val['id']));
      $xmlChild->appendChild($xmlInfo);
      
      $xmlReport = $xml->createElement("reportpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=report&tid='.$val['id']));
      $xmlChild->appendChild($xmlReport);
      
      $xmlCurrent = $xml->createElement("xmlcurrent",'http://www.phuketinnova.com/demo/gthai/pv00001.xml');
      $xmlChild->appendChild($xmlCurrent);
      
      $xmlPath = $xml->createElement("xmlpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=village&lid='.$val['id']));
      $xmlChild->appendChild($xmlPath);
      
      $xmlUpdate = $xml->createElement("xmlupdate",'DistrictData');
      $xmlChild->appendChild($xmlUpdate);
      
      $xmlDate = $xml->createElement("date",$val['lu_date']);
      $xmlChild->appendChild($xmlDate);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
  }else{
    //other case, we don't show anything.
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("error");
    $xmlChild = $xml->createElement("message",'Inccorect argument:[did],[lid] are require.');
    $xmlDatas->appendChild($xmlChild);
  
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  
  pnShutDown();
  
}


function VotePortal_xml_village()
{
  $lid  = FormUtil::getPassedValue ('lid', null , 'GET');
  $vid  = FormUtil::getPassedValue ('vid', null , 'GET');
  $page  = (FormUtil::getPassedValue ('page', null , 'GET')) ? FormUtil::getPassedValue ('page', null , 'GET') : 0;
  $pagesize = (FormUtil::getPassedValue ('pagesize', null , 'GET')) ? FormUtil::getPassedValue ('pagesize', null , 'GET') : 1000;
  
  if((!isset($lid)&&isset($vid))){
    //if assign $lid (means, reguester want to see detail of it) render district list of this province.
    if (!($class = Loader::loadClassFromModule ('VotePortal','Village', true)))
    return LogUtil::registerError ("Unable to load class [Village] ...");

    $object = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($vid)){
      if (method_exists($object,'genFilter')){
        $where = $object->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$object->_objWhere);
    }else if (is_array($object->_objWhere)){
      $where .= implode(' AND ',$object->_objWhere);
    }else if (!empty($object->_objWhere)){
      $where .= ' AND ' . $object->_objWhere;
    }
    if (empty($sort)){
      $sort = $object->_objSort;
    }
    $object->get($where);
    $objData = $object->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    $i=0;
    foreach($objData as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id",++$i);
  
      $xmlTitle = $xml->createElement("title","infomation of ม.".$val['moo']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlDes = $xml->createElement("description","Description of ม.".$val['moo']." (หมู่บ้าน".$val['name'].")");
      $xmlChild->appendChild($xmlDes);
      
      $xmlDetail = $xml->createElement("detail","Detail of ม.".$val['moo']." (หมู่บ้าน".$val['name'].")");
      $xmlChild->appendChild($xmlDetail);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
    
  }else if(isset($lid)&&(!isset($vid))){
    //if not assign $lid, some time input incorrect $lid it's not comes to this case.
    if (!($class = Loader::loadClassFromModule ('VotePortal','VillageArray', false)))
    return LogUtil::registerError ("Unable to load class [VillageArray] ...");

    $objectArray = new $class ();
    $where   = null;
    if(isset($lid)){
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$objectArray->_objWhere);
    }else if (is_array($objectArray->_objWhere)){
      $where .= implode(' AND ',$objectArray->_objWhere);
    }else if (!empty($objectArray->_objWhere)){
      $where .= ' AND ' . $objectArray->_objWhere;
    }
    if (empty($sort)){
      $sort = $objectArray->_objSort;
    }
    $objectArray->get ($where);
    $objDataArray = $objectArray->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    
    if(empty($objDataArray))
    {
      $xmlChild = $xml->createElement("data");
      $xmlChild->setAttribute("id",0);
      $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
      $xmlChild->appendChild($xmlTitle);
      $xmlDatas->appendChild($xmlChild);
      $xml->appendChild($xmlDatas);
    
      print $xml->saveXML();
      pnShutDown();
    }
    
    foreach($objDataArray as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id","vl".sprintf("%06d",$val['id']));
  
      $xmlTitle = $xml->createElement("title","หมู่บ้าน ".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlPop = $xml->createElement("population","300");
      $xmlChild->appendChild($xmlPop);
      
      $xmlLat = $xml->createElement("latitude",$val['latitude']);
      $xmlChild->appendChild($xmlLat);
      
      $xmlLon = $xml->createElement("longitude",$val['longitude']);
      $xmlChild->appendChild($xmlLon);
      
      $xmlSpan = $xml->createElement("span",0.1);
      $xmlChild->appendChild($xmlSpan);
      
      $xmlInfo = $xml->createElement("infopath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=village&vid='.$val['id']));
      $xmlChild->appendChild($xmlInfo);
      
      $xmlReport = $xml->createElement("reportpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=report&vid='.$val['id']));
      $xmlChild->appendChild($xmlReport);
      
      $xmlCurrent = $xml->createElement("xmlcurrent",'http://www.phuketinnova.com/demo/gthai/pv00001.xml');
      $xmlChild->appendChild($xmlCurrent);
      
      $xmlPath = $xml->createElement("xmlpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=place&sort=date&vid='.$val['id']));
      $xmlChild->appendChild($xmlPath);
      
      $xmlUpdate = $xml->createElement("xmlupdate",'DistrictData');
      $xmlChild->appendChild($xmlUpdate);
      
      $xmlDate = $xml->createElement("date",$val['lu_date']);
      $xmlChild->appendChild($xmlDate);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
  }else{
    //other case, we don't show anything.
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("error");
    $xmlChild = $xml->createElement("message",'Inccorect argument:[lid],[vid] are require.');
    $xmlDatas->appendChild($xmlChild);
  
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  
  pnShutDown();
  
}


function VotePortal_xml_place()
{
  $vid  = FormUtil::getPassedValue ('vid', null , 'GET');
  
  
  if(isset($vid)){
    //if assign $vid (means, reguester want to see detail of it) render district list of this province.    
    
    if (!($class = Loader::loadClassFromModule ('VotePortal','HouseNo', true)))
    return LogUtil::registerError ("Unable to load class [HouseNo] ...");


    $objectArray = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($vid)){
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$objectArray->_objWhere);
    }else if (is_array($objectArray->_objWhere)){
      $where .= implode(' AND ',$objectArray->_objWhere);
    }else if (!empty($objectArray->_objWhere)){
      $where .= ' AND ' . $objectArray->_objWhere;
    }
    
    $sort = $objectArray->genSort();

    $objectArray->get($where, $sort);
    $objData = $objectArray->_objData;
    
    
    $object = new $class ();
    $where   = null;
    $sort = null;
    
    if (!($class = Loader::loadClassFromModule ('VotePortal','Village', true)))
    return LogUtil::registerError ("Unable to load class [Village] ...");

    $object = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($vid)){
      if (method_exists($object,'genFilter')){
        $where = $object->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$object->_objWhere);
    }else if (is_array($object->_objWhere)){
      $where .= implode(' AND ',$object->_objWhere);
    }else if (!empty($object->_objWhere)){
      $where .= ' AND ' . $object->_objWhere;
    }
    if (empty($sort)){
      $sort = $object->_objSort;
    }
    $object->get($where);
    $objDataVillage = $object->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    
    if(empty($objDataVillage))
    {
      $xmlChild = $xml->createElement("data");
      $xmlChild->setAttribute("id",0);
      $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
      $xmlChild->appendChild($xmlTitle);
      $xmlDatas->appendChild($xmlChild);
      $xml->appendChild($xmlDatas);
    
      print $xml->saveXML();
      pnShutDown();
    }
    
    
    $xmlChild = $xml->createElement("data");
    
    $xmlChild->setAttribute("id","pc".sprintf("%06d",$objDataVillage[0]['id']));

    $xmlTitle = $xml->createElement("title","place home");
    $xmlChild->appendChild($xmlTitle);
    
    $xmlDes = $xml->createElement("description","Description of ".$objDataVillage[0]['name']);
    $xmlChild->appendChild($xmlDes);
    
    $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
    $xmlChild->appendChild($xmlLogo);
    
    $xmlType = $xml->createElement("type","home");
    $xmlChild->appendChild($xmlType);
    
    $xmlPub = $xml->createElement("publisher","publisher");
    $xmlChild->appendChild($xmlPub);
    
    $xmlPath = $xml->createElement("xmlpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=home&vid='.$vid));
    $xmlChild->appendChild($xmlPath);
    
    $xmlUpdate = $xml->createElement("xmlupdate",'DistrictData');
    $xmlChild->appendChild($xmlUpdate);
    
    $xmlVersion = $xml->createElement("version","1");
    $xmlChild->appendChild($xmlVersion);
    
    $xmlDate = $xml->createElement("date",$objData[0]['lu_date']);
    $xmlChild->appendChild($xmlDate);
    
    $xmlDatas->appendChild($xmlChild);
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
  }else{
    //other case, we don't show anything.
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("error");
    $xmlChild = $xml->createElement("message",'Inccorect argument:[vid],[hid] are require.');
    $xmlDatas->appendChild($xmlChild);
  
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  
  pnShutDown();
  
}


function VotePortal_xml_home()
{
  $vid  = FormUtil::getPassedValue ('vid', null , 'GET');
  
  if((!isset($vid)&&isset($hid))){
    //if assign $vid (means, reguester want to see detail of it) render district list of this province.
    if (!($class = Loader::loadClassFromModule ('VotePortal','HouseNo', true)))
    return LogUtil::registerError ("Unable to load class [HouseNo] ...");

    $object = new $class ();
    $where   = null;
    $sort = null;
    
    if(isset($hid)){
      if (method_exists($object,'genFilter')){
        $where = $object->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$object->_objWhere);
    }else if (is_array($object->_objWhere)){
      $where .= implode(' AND ',$object->_objWhere);
    }else if (!empty($object->_objWhere)){
      $where .= ' AND ' . $object->_objWhere;
    }
    if (empty($sort)){
      $sort = $object->_objSort;
    }
    $object->get($where);
    $objData = $object->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    
    $i=0;
    foreach($objData as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id",++$i);
  
      $xmlTitle = $xml->createElement("title","infomation of ".$val['name']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlLogo = $xml->createElement("logo",htmlentities('http://www.phuketinnova.com/demo/gthai/911thai.png'));
      $xmlChild->appendChild($xmlLogo);
      
      $xmlDes = $xml->createElement("description","Description of ".$val['name']);
      $xmlChild->appendChild($xmlDes);
      
      $xmlDetail = $xml->createElement("detail","Detail of ".$val['name']);
      $xmlChild->appendChild($xmlDetail);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
    
  }else if(isset($vid)&&(!isset($hid))){
    //if not assign $vid, some time input incorrect $vid it's not comes to this case.
    if (!($class = Loader::loadClassFromModule ('VotePortal','HouseNoArray', false)))
    return LogUtil::registerError ("Unable to load class [HouseNoArray] ...");

    $objectArray = new $class ();
    $where   = null;
    if(isset($vid)){
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
	  }
    if (empty($where)){
      $where = implode(' AND ',$objectArray->_objWhere);
    }else if (is_array($objectArray->_objWhere)){
      $where .= implode(' AND ',$objectArray->_objWhere);
    }else if (!empty($objectArray->_objWhere)){
      $where .= ' AND ' . $objectArray->_objWhere;
    }
    if (empty($sort)){
      $sort = $objectArray->_objSort;
    }
    $objectArray->get ($where);
    $objDataArray = $objectArray->_objData;
    
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("datas");
    if(empty($objDataArray))
    {
      $xmlChild = $xml->createElement("data");
      $xmlChild->setAttribute("id",0);
      $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
      $xmlChild->appendChild($xmlTitle);
      $xmlDatas->appendChild($xmlChild);
      $xml->appendChild($xmlDatas);
    
      print $xml->saveXML();
      pnShutDown();
    }
    foreach($objDataArray as $val)
    {
      
      $xmlChild = $xml->createElement("data");
      
      $xmlChild->setAttribute("id","hm".sprintf("%06d",$val['id']));
  
      $xmlTitle = $xml->createElement("title","บ้านเลขที่ ".$val['no']);
      $xmlChild->appendChild($xmlTitle);
      
      $xmlIcon = $xml->createElement("icon",htmlentities('http://www.phuketinnova.com/demo/gthai/home.png'));
      $xmlChild->appendChild($xmlIcon);
      
      $xmlPep = $xml->createElement("people",$val['unit']);
      $xmlChild->appendChild($xmlPep);
      
      $xmlLat = $xml->createElement("latitude",$val['latitude']);
      $xmlChild->appendChild($xmlLat);
      
      $xmlLon = $xml->createElement("longitude",$val['longitude']);
      $xmlChild->appendChild($xmlLon);
      
      
      //Who is in this home link, it'll link to this function but assign $hid 
      $xmlPath = $xml->createElement("xmlpath",htmlentities('http://www.phuketinnova.com/demo/gthai/person.xml'));
      $xmlChild->appendChild($xmlPath);
      
      $xmlUpdate = $xml->createElement("xmlupdate",'DistrictData');
      $xmlChild->appendChild($xmlUpdate);
      
      $xmlDate = $xml->createElement("date",$val['lu_date']);
      $xmlChild->appendChild($xmlDate);
      
      $xmlDatas->appendChild($xmlChild);
    }
  
    $xml->appendChild($xmlDatas);
    
    
    print $xml->saveXML();
    pnShutDown();
  }else{
    //other case, we don't show anything.
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
    
    $xmlDatas = $xml->createElement("error");
    $xmlChild = $xml->createElement("message",'Inccorect argument:[vid],[hid] are require.');
    $xmlDatas->appendChild($xmlChild);
  
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  
  pnShutDown();
  
}

function VotePortal_xml_report()
{
  $reportHost = "http://www.911thai.com/";
  
  $pid  = FormUtil::getPassedValue ('pid', null , 'GET');
  $did  = FormUtil::getPassedValue ('did', null , 'GET');
  $tid  = FormUtil::getPassedValue ('tid', null , 'GET');
  $vid  = FormUtil::getPassedValue ('vid', null , 'GET');
  $page  = FormUtil::getPassedValue ('page', null , 'GET');
  
  if(intval($pid)&&isset($pid))
  {
    $args = "&pid=".$pid;
    $rpId = $pid;
  }
  if(intval($did)&&isset($did))
  {
    $args = "&did=".$did;
    $rpId = $did;
  }
  if(intval($tid)&&isset($tid))
  {
    $args = "&tid=".$tid;
    $rpId = $tid;
  }
  if(intval($vid)&&isset($vid))
  {
    $args = "&vid=".$vid;
    $rpId = $vid;
  }
  
  $logoid = substr($rpId,0,2);
  
  $Maplink = $reportHost."index.php?module=VotePetition&type=xml&func=list".$args;
  
  $xmlMap = @file_get_contents($Maplink);
  
  $pxml = simplexml_load_string($xmlMap);

  if($pxml->totalPages==0)
  {
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
  
    $xmlDatas = $xml->createElement("datas");
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  
  for($i=1;$i<=$pxml->totalPages;$i++){
    
    $Maplink = $reportHost."index.php?module=VotePetition&type=xml&func=list".$args."&page=".$i;
    $xmlMap = @file_get_contents($Maplink);
    $pagexml = simplexml_load_string($xmlMap);
    
    $xmlChild = $xml->createElement("data");
  
    $xmlChild->setAttribute("id","rp".$i.sprintf("%06d",$rpId));
  
    $xmlTitle = $xml->createElement("title","ข้อมูลปัญหา ชุดที่ ".$i);
    $xmlChild->appendChild($xmlTitle);
    
    $xmlDes = $xml->createElement("description","ข้อมูลปัญหา ชุดที่ ".$i);
    $xmlChild->appendChild($xmlDes);
    
    $xmlLogo = $xml->createElement("logo",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'modules/VotePortal/pnimages/province/'.$logoid.".png"));
    $xmlChild->appendChild($xmlLogo);
    
    $xmlType = $xml->createElement("type","map");
    $xmlChild->appendChild($xmlType);
    
    $xmlPub = $xml->createElement("publisher",htmlentities($reportHost));
    $xmlChild->appendChild($xmlPub);
    
    $xmlCur = $xml->createElement("xmlcurrent",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=report'.$args));
    $xmlChild->appendChild($xmlCur);
    
    $xmlPath = $xml->createElement("xmlpath",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'index.php?module=VotePortal&type=xml&func=reportmap'.$args.'&page='.$i));
    $xmlChild->appendChild($xmlPath);
    
    $xmlUpdate = $xml->createElement("xmlupdate",htmlentities('http://www.911thai.com/demo/update.xml'));
    $xmlChild->appendChild($xmlUpdate);
    
    $xmlVer = $xml->createElement("version",1);
    $xmlChild->appendChild($xmlVer);
    
    //$xmlDate = $xml->createElement("date",date("Y-m-d H:i:s"));
    $xmlDate = $xml->createElement("date",$pagexml->updateTime);
    $xmlChild->appendChild($xmlDate);
    
    $xmlDatas->appendChild($xmlChild);
  }
 
  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
}


function VotePortal_xml_reportmap() {

  $reportHost = "http://www.911thai.com/";
  
  $pid  = FormUtil::getPassedValue ('pid', null , 'GET');
  $did  = FormUtil::getPassedValue ('did', null , 'GET');
  $tid  = FormUtil::getPassedValue ('tid', null , 'GET');
  $vid  = FormUtil::getPassedValue ('vid', null , 'GET');
  $page = FormUtil::getPassedValue ('page', null , 'GET');
  
  if(intval($pid)&&isset($pid))
  {
    $args = "&pid=".$pid;
    if(intval($page)&&isset($page))
    $args .= "&page=".$page;
  }
  if(intval($did)&&isset($did))
  {
    $args = "&did=".$did;
    if(intval($page)&&isset($page))
    $args .= "&page=".$page;
  }
  if(intval($tid)&&isset($tid))
  {
    $args = "&tid=".$tid;
    if(intval($page)&&isset($page))
    $args .= "&page=".$page;
  }
  if(intval($vid)&&isset($vid))
  {
    $args = "&vid=".$vid;
    if(intval($page)&&isset($page))
    $args .= "&page=".$page;
  }
  
  $Maplink = $reportHost."index.php?module=VotePetition&type=xml&func=list".$args;
  
  $xmlMap = @file_get_contents($Maplink);
  
  $pxml = simplexml_load_string($xmlMap);


  if($pxml->totalPages==0)
  {
    header("Content-type: text/xml");
    $xml = new DOMDocument("1.0", "utf-8" );
    $xml->preserveWhiteSpace = false; 
    $xml->formatOutput = true;
  
    $xmlDatas = $xml->createElement("datas");
    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",0);
    
    $xmlTitle = $xml->createElement("title","ไม่มีข้อมูล");
    $xmlChild->appendChild($xmlTitle);
    
    $xmlDatas->appendChild($xmlChild);
    $xml->appendChild($xmlDatas);
    print $xml->saveXML();
    pnShutDown();
  }
  
  header("Content-type: text/xml");
  $xml = new DOMDocument("1.0", "utf-8" );
  $xml->preserveWhiteSpace = false; 
  $xml->formatOutput = true;
  
  $xmlDatas = $xml->createElement("datas");
  $i=0;

  foreach($pxml->item AS $item){

    $xmlChild = $xml->createElement("data");
    $xmlChild->setAttribute("id",sprintf("%06d",++$i));
    
    $xmlTitle = $xml->createElement("title","บ.".$item->house." ".$item->title);
    $xmlChild->appendChild($xmlTitle);
    
    $xmlIcon = $xml->createElement("icon",htmlentities(str_replace(basename($_SERVER['PHP_SELF']),"","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']).'modules/VotePortal/pnimages/icons/home.png'));
    $xmlChild->appendChild($xmlIcon);
    
    $xmlDetail = $xml->createElement("detail",$item->description);
    $xmlChild->appendChild($xmlDetail);
    
    $xmlLat = $xml->createElement("latitude",$item->latitude);
    $xmlChild->appendChild($xmlLat);
    
    $xmlLat = $xml->createElement("longitude",$item->longitude);
    $xmlChild->appendChild($xmlLat);
    
    $xmlDatas->appendChild($xmlChild);
  }
 
  $xml->appendChild($xmlDatas);
  
  
  print $xml->saveXML();
  pnShutDown();
}